package sky.cin.framework.ioms.registry.center.entities;

import sky.cin.framework.ioms.common.entities.*;

import java.util.Arrays;
import java.util.Map;

public class InvokeInfo {

    private int pooling;
    MicroService[] microServices;

    public ServiceEndPoint select(Map<String, MicroService> msMap, LoadBalance balance) {
        MicroService ms = null;
        switch (balance) {
            case POLLING: {
                MicroService[] microServices = msMap.values().toArray(new MicroService[0]);
                if (!Arrays.equals(this.microServices, microServices)) {
                    this.microServices = microServices;
                    this.pooling = 0;
                }
                ms = microServices[pooling()];
                break;
            }
            default: {
            }
        }
        return new ServiceEndPoint(ms.getDomain(), ms.getPort());
    }

    private int pooling() {
        return (pooling++) % microServices.length;
    }
}
